package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.R;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.viewpager.extensions.FixedTabsView;
import com.astuetz.viewpager.extensions.TabsAdapter;
import com.astuetz.viewpager.extensions.ViewPagerTabButton;

public class MainFragment extends BaseFragment{
	public static final String TAG="MainFragment";
	
	private ViewPager mViewPager;
	private FixedTabsView mFixedTabsView;
	
	private static final String FLOOR_ID="floor_id";
	private static final String ROOM_ID="room_id";
	//Khai bao bien luu floor_id vs room_id
	private static int mFloor_id;
	private static int mRoom_id;
	
	//Cac bien chua thong tin ve loai man hinh
	private static int SCREEN_DEVICE=0;
	private static int SCREEN_SCENE=1;
	private static int SCREEN_TOTAL=2;
	
	private MainFragmentPagerAdapter mMainFragmentPagerAdapter;
	private static DeviceFragment mDeviceFragment;
	private static SceneFragment mSceneFragment;
	
	private OnTabSelectedListener mOnTabSelectedListener;
	/**
	 * 
	 * @param floor_id
	 * @param room_id
	 * @return
	 */
	public static MainFragment newInstance(int floor_id,int room_id){
		MainFragment sMainFragment=new MainFragment();
		Bundle bundle=new Bundle();
		bundle.putInt(FLOOR_ID, floor_id);
		bundle.putInt(ROOM_ID, room_id);
		sMainFragment.setArguments(bundle);
		return sMainFragment;
	}
	
	@Override
	public void onAttach(Activity activity) {
		super.onAttach(activity);
		mOnTabSelectedListener=(OnTabSelectedListener)activity;
	}
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		mFloor_id=getArguments().getInt(FLOOR_ID);
		mRoom_id=getArguments().getInt(ROOM_ID);
	}
	
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view=inflater.inflate(R.layout.fragment_main, container, false);
		initViews(view);
		initFragments(mFloor_id, mRoom_id);
		initDatas();
		return view;
	}
	@Override
	public void onResume() {
		super.onResume();
	}
	
	private void initViews(View view){
		mViewPager=(ViewPager)view.findViewById(R.id.fragment_main_viewpager);
		mFixedTabsView=(FixedTabsView)view.findViewById(R.id.fragment_main_fixed_tabs);
	}
	
	private void initFragments(int floor_id,int room_id){
		mDeviceFragment=DeviceFragment.newInstance(floor_id, room_id);
		mSceneFragment=SceneFragment.newInstance(floor_id, room_id);
	}
	private void initDatas(){
		mFixedTabsView.setAdapter(new FixedTabsAdapter(getActivity()));
		mMainFragmentPagerAdapter=new MainFragmentPagerAdapter(getFragmentManager());
		mViewPager.setAdapter(mMainFragmentPagerAdapter);
		mFixedTabsView.setViewPager(mViewPager);
		
        
		mViewPager.setOnPageChangeListener(new OnPageChangeListener() {
			
			@Override
			public void onPageSelected(int arg0) {
				if(SCREEN_SCENE==arg0){
					mOnTabSelectedListener.onTabSelected(TABS.SCENE);
				}else if(SCREEN_DEVICE==arg0){
					mOnTabSelectedListener.onTabSelected(TABS.DEVICE);
				}
			}
			
			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}
			
			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});
		mViewPager.setCurrentItem(SCREEN_DEVICE, true);
	}
	@Override
	public void refreshData(int floor_id,int room_id){
		mFloor_id=floor_id;
		mRoom_id=room_id;
		mDeviceFragment.refreshData(floor_id, room_id);
		mSceneFragment.refreshData(floor_id, room_id);
		mViewPager.setCurrentItem(SCREEN_DEVICE,true);
	}
	
	private static class FixedTabsAdapter implements TabsAdapter{
		private Activity mActivity;
		private String mTitles[];
		public FixedTabsAdapter(Activity activity) {
			mActivity=activity;
			mTitles=activity.getResources().getStringArray(R.array.tabs);
		}
		@Override
		public View getView(int position) {
			ViewPagerTabButton tab;
			
			LayoutInflater inflater = mActivity.getLayoutInflater();
			tab = (ViewPagerTabButton) inflater.inflate(R.layout.item_fragment_main_tab_fixed, null);
			
			if (position < mTitles.length) tab.setText(mTitles[position]);
			
			return tab;
		}
		
	}
	
	private static class MainFragmentPagerAdapter extends FragmentPagerAdapter{
		public MainFragmentPagerAdapter(FragmentManager fm) {
			super(fm);
		}

		@Override
		public Fragment getItem(int arg0) {
			Fragment fragment;
			if(arg0==SCREEN_SCENE){
				fragment=mSceneFragment;
			}else{
				fragment=mDeviceFragment;
			}
			return fragment;
		}

		@Override
		public int getCount() {
			return SCREEN_TOTAL;
		}
	}
	
	public enum TABS{
		SCENE,
		DEVICE
	}
	public interface OnTabSelectedListener{
		public void onTabSelected(TABS tab);
	}
}
